import Vue from 'vue'
import Router from 'vue-router'


Vue.use(Router)

const Login = () => import('../pages/Login')
const Layout = () => import('../pages/Layout')
const Index = () => import('../pages/menu/Index')
const Role=()=>import("../pages/role/Role")
const Admin=()=>import("../pages/admin/Admin")
const Sort=()=>import('../pages/sort/Sort')
const Routers = [
  {
    path: '/login',
    name: "login",
    component: Login,
    meta: {
      title: "登陆"
    }
  },
  {
    path: "/",
    name: "layout",
    component: Layout,
    children: [
      {
        path: '/sort',
        name: 'sort',
        component: Sort,
        meta: {
          title: "商品分类"
        }
      },
      {
        path: '/menu',
        name: 'menu',
        component: Index,
        meta: {
          title: "菜单管理"
        }
      },
      {
        path: "/role",
        name: "role",
        component: Role,
        meta: {
          title: "角色管理"
        }
      },
      {
        path: "/admin",
        name: "admin",
        component: Admin,
        meta: {
          title: "管理员管理"
        }
      }
    ],
    meta:{
      title:"网站首页"
    }
  }
]
const router = new Router({
  routes: Routers
})
router.beforeEach(
  function (to, from, next) {
    if (to.path != "/login") {
      const token = JSON.parse(localStorage.getItem('userData')) || {};
      if (!token) {
        return next('/login')
      }

    }  
    next()
    document.title = to.meta.title
  }
)
export default router